package handler

import (
	"github.com/gin-gonic/gin"
	"logi-flow/cache"
	"logi-flow/model"
	"logi-flow/pkg"
	"strconv"
	"time"
)

func UserRoleList(c *gin.Context) {
	var list []model.UserRoleList
	/*
		SELECT
		  users.username Username,
		  users.`password` Password,
		  roles.role_name RoleName
		FROM
		  user_roles
		  LEFT JOIN users ON user_roles.uid = users.id
		  LEFT JOIN roles ON user_roles.rid = roles.id
	*/
	model.DB.Table("user_roles").
		Select(
			"users.username Username",
			"users.`password` Password",
			"roles.role_name RoleName",
		).
		Joins("LEFT JOIN users ON user_roles.uid = users.id").
		Joins("LEFT JOIN roles ON user_roles.rid = roles.id").
		Find(&list)
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "ok",
		"data": list,
	})
	return
}

func UserRoleAdd(c *gin.Context) {
	uid, _ := strconv.Atoi(c.PostForm("uid"))
	rid, _ := strconv.Atoi(c.PostForm("rid"))
	data := model.UserRole{
		Uid: uid,
		Rid: rid,
	}
	if err := model.DB.Create(&data).Error; err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "添加失败",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"mag":  "添加成功",
	})
}

func PersonOrderSelect(c *gin.Context) {
	id := c.Query("id")
	var list []model.PerOrderList
	/*
		SELECT
		  per_orders.id Id,
		  per_orders.order_no OrderNo,
		  per_orders.order_to OrderTo,
		  people.per_no PerNo,
		  people.per_name PerName
		FROM
		  per_orders
		  LEFT JOIN people ON per_orders.pid = people.id
		WHERE per_orders.id=1
	*/
	model.DB.Table("per_orders").
		Select(
			"per_orders.id OrderId",
			"per_orders.order_no OrderNo",
			"per_orders.order_to OrderTo",
			"people.per_no PerNo",
			"people.per_name PerName",
		).
		Joins("LEFT JOIN people ON per_orders.pid = people.id").
		Where("per_orders.id=?", id).
		Find(&list)
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "OK",
		"data": list,
	})
}

func UserRegister(c *gin.Context) {
	username := c.PostForm("username")
	password := c.PostForm("password")
	if username == "" || password == "" {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "字段不能为空",
		})
		return
	}
	data := model.User{
		Username: username,
		Password: pkg.Md5(password),
	}
	if err := model.DB.Create(&data).Error; err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"mag":  "注册失败",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"mag":  "注册成功",
	})
	return
}

func UserLogin(c *gin.Context) {
	username := c.PostForm("username")
	password := c.PostForm("password")
	if username == "" || password == "" {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "字段不能为空",
		})
		return
	}
	redis_key := "user_login_" + username
	errCount, _ := cache.Rdb.Get(cache.Ctx, redis_key).Int()
	if errCount >= 3 {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "错误三次已封禁",
		})
		return
	}
	var user model.User
	if err := model.DB.Where("username=?", username).Find(&user).Error; err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "查询用户失败",
		})
		return
	}
	if user.ID == 0 {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "用户不存在",
		})
		return
	}
	if pkg.Md5(password) != user.Password {
		cache.Rdb.Incr(cache.Ctx, redis_key)
		if errCount == 2 {
			cache.Rdb.Set(cache.Ctx, redis_key, 3, time.Hour*2)
			c.JSON(200, gin.H{
				"code": 500,
				"msg":  "密码错误三次账号禁封两小时",
			})
			return
		}
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "密码错误,重新输入",
		})
		return
	}
	token := pkg.TokenHandler(username)
	c.JSON(200, gin.H{
		"code":  200,
		"msg":   "登陆成功",
		"data":  user,
		"token": token,
	})
}

func RoleNode(c *gin.Context) {
	rid := c.Query("rid")
	/*
			SELECT
		  *
		FROM
		  nodes
		  LEFT JOIN role_nodes ON nodes.id = role_nodes.nid
		WHERE role_nodes.rid=1
	*/
	var nodes model.Node
	model.DB.Table("nodes").
		Joins("LEFT JOIN role_nodes ON nodes.id = role_nodes.nid").
		Where("role_nodes.rid=?", rid).
		Find(&nodes)
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "ok",
		"data": nodes,
	})
	return
}

func UserRole(c *gin.Context) {
	uid := c.Query("uid")
	/*
		SELECT
		  *
		FROM
		  roles
		  LEFT JOIN user_roles ON roles.id = user_roles.rid
		WHERE user_roles.uid=1
	*/
	var role model.Role
	model.DB.Table("roles").
		Joins("LEFT JOIN user_roles ON roles.id = user_roles.rid").
		Where("user_roles.uid=?", uid).
		Find(&role)
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "ok",
		"data": role,
	})
	return
}

func RoleNodeAdd(c *gin.Context) {
	rid, _ := strconv.Atoi(c.PostForm("rid"))
	nid, _ := strconv.Atoi(c.PostForm("nid"))
	data := model.RoleNode{
		Rid: rid,
		Nid: nid,
	}
	if err := model.DB.Create(&data).Error; err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "添加失败",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"mag":  "添加成功",
	})
	return
}

func Login(c *gin.Context) {
	username := c.PostForm("username")
	password := c.PostForm("password")
	if username == "" || password == "" {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "字段不能为空",
		})
		return
	}
	redis_key := "login_" + username
	errCount, _ := cache.Rdb.Get(cache.Ctx, redis_key).Int()
	if errCount >= 3 {
		c.JSON(200, gin.H{
			"code": 500,
			"mag":  "错误三次以上已封禁",
		})
		return
	}
	var user model.User
	if err := model.DB.Where("username=?", username).Find(&user).Error; err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"mag":  "查询用户失败",
		})
		return
	}
	if user.ID == 0 {
		c.JSON(200, gin.H{
			"code": 500,
			"mag":  "用户不存在",
		})
		return
	}
	if pkg.Md5(password) != user.Password {
		cache.Rdb.Incr(cache.Ctx, redis_key)
		if errCount == 2 {
			cache.Rdb.Set(cache.Ctx, redis_key, 3, time.Hour*2)
			c.JSON(200, gin.H{
				"code": 500,
				"msg":  "错误三次封禁两小时",
			})
			return
		}
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "密码错误重新输入",
		})
		return
	}
	token := pkg.TokenHandler(strconv.Itoa(int(user.ID)))
	c.JSON(200, gin.H{
		"code":  200,
		"msg":   "登录成功",
		"data":  user,
		"token": token,
	})
	return
}
func Info(c *gin.Context) {
	uid, _ := c.Get("uid")
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "OK",
		"uid":  uid,
	})
	return
}
